import tkinter as tk
from tkinter import filedialog
import requests
import bs4
import re
import os
import threading
import easygui
path=''
root = tk.Tk()
E1 = tk.Entry(root, bd=5)
E1.place(x=50, y=5)
T=tk.Text(root,width=24,height=5.4,bg='white')
T.place(x=2,y=100)
class pa:
    path=''
    def __init__(self, path=r'.\\'):  # 构造函数或类的初始化，创建实例时会被调用
        self.path=path
    id_item = []
    titles=[]
    urls=['https://www.tiny.com.hk/shop?tag=%E6%B6%88%E9%98%B2','https://www.tiny.com.hk/shop?tag=%E6%95%91%E8%AD%B7','https://www.tiny.com.hk/shop?tag=%E8%AD%A6%E5%AF%9F'
          ,'https://www.tiny.com.hk/shop?tag=%E5%B7%B4%E5%A3%AB','https://www.tiny.com.hk/shop?tag=%E5%85%AC%E5%85%B1%E4%BA%A4%E9%80%9A','https://www.tiny.com.hk/shop?tag=%E5%B0%8F%E5%B7%B4%E7%89%8C',
          'https://www.tiny.com.hk/shop?tag=%E5%8F%B0%E7%81%A3','https://www.tiny.com.hk/shop?tag=%E6%BE%B3%E9%96%80','https://www.tiny.com.hk/shop?tag=Mini%20GT&per_page=150','https://www.tiny.com.hk/shop?tag=%E6%BE%B3%E9%96%80']
    def run(self):
        for url in self.urls:
            self.creatpic(url)
    def creatpic(self,url):
        id_item=[]
        url = url
        headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.69 Safari/537.36'}
        response = requests.get(url, headers=headers)
        soup = bs4.BeautifulSoup(response.text, 'html.parser')
        items = soup.find_all(class_='woocommerce-LoopProduct-link woocommerce-loop-product__link')
        for item in items:
            _id = re.findall(r'=([0-9]{5})', str(item))[0]
            id_item.append(_id)
        for item in id_item:
            url1='https://www.tiny.com.hk/product?product_id='+item
            print(url1)
            headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.69 Safari/537.36'}
            response1 = requests.get(url1, headers=headers)

            soup = bs4.BeautifulSoup(response1.text, 'html.parser')
            temTxtFile = str(soup.find_all('pre'))
            txtFile = str(re.findall('>(.*)<', temTxtFile, re.S)[0])
            temMoreData2 = str(soup.find_all(class_='product_meta')[0])
            moreData2 = re.findall(r'>(.{0,15})</a>', temMoreData2)
            sku = str(re.findall(r'(SKU:.*)</div>', temMoreData2, re.S)[0]).strip()
            temtitle = str(soup.find_all(class_='product_title entry-title'))
            title=str(re.findall(r'>(.*?)<',temtitle,re.S)[0]).strip()+' '+sku

            rstr = r"[\/\\\:\*\?\"\<\>\|]"
            new_title = re.sub(rstr, "_", title)
            if not os.path.exists(new_title):
                if len(new_title)>60:
                    new_title=new_title[0:60].strip()
                print(new_title)
                os.mkdir(new_title)
            tempic=str(soup.find_all(class_='sp-wrap'))
            picurls=re.findall('href="(.*)"><img',tempic)
            for url in picurls:
                filename = self.path+'\\'+new_title+'\\'+url.split('/')[-1]
                print(filename)
                T.insert(tk.END,filename+'爬取完成'+'\n')
                with open(filename, 'wb') as f:
                    response = requests.get(url, headers=headers)
                    f.write(response.content)
            txtFileName=self.path + '\\' + new_title+'\\'+ new_title+'.txt'
            temWight=str(soup.find_all(class_='product_weight')[0])
            weight='\n重量: '+str(re.findall(r'>(.*)<',temWight,re.S)[0]).strip()
            temMoreData1=str(soup.find_all(class_='posted_in'))
            moreData1=re.findall(r'<!-- (.*)-->?',temMoreData1)
            with open(txtFileName,'w+') as f:
                f.write(txtFile)
                f.write(weight+'\n')
                for item in moreData1:
                    f.write(item)
                f.write('\n標籤：')
                for item in moreData2:
                    f.write(item+' ')
                f.write('\n'+sku)
            self.titles.append(new_title)
        print(self.titles)
data=''
def thread_it(func, *args):
    '''将函数打包进线程'''
    t = threading.Thread(target=func, args=args)
    t.setDaemon(True)
    t.start()

def checkfile():
    path = str(easygui.diropenbox())
    print(path)
    pachong = pa(path)
    pachong.run()
def win():
    '''打开选择文件夹对话框'''
    L1 = tk.Label(root, text="关键字")
    L1.place(x=5,y=5)
    B2 = tk.Button(root, text="选择路径", command=lambda :thread_it(checkfile))
    B2.place(x=5,y=50)
    root.mainloop()
win()
